﻿using System.IO;
using Newtonsoft.Json.Linq;

namespace WebAPI.Utils.wechat;

public class Token
{
    // 读取TOKEN
    public static string Read(string fileName)
    {
        if (!File.Exists(fileName))
        {
            Write(fileName);
        }
        return FileUtil.Read(fileName);
    }

    // 保存TOKEN
    public static void Write(string fileName, string access_token = "", long expires_in = 0)
    {
        //string json = @"{ 'access_token': '', 'expires_in': 0}";
        //WriteFile(fileName, json);
        JObject obj = new()
        {
            ["access_token"] = access_token,
            ["expires_in"] = expires_in
        };
        FileUtil.Write(fileName, obj.ToString());
    }
}